<!-- 修改新闻信息 -->
<template>
  <div class="updateNotice" v-loading='loading'>
    <div class="left-aside" :style="{minWidth: device=='mobile'?'100%': ''}">
      <el-form :model="form" ref="form" :rules="rules" label-width="80px" size="small">
        <el-row :gutter="20">
          <el-col :xs='24' :sm='24' :md='24' :lg='12' :xl='12'>
            <el-form-item label="标 题" prop="title">
              <el-input v-model="form.title" placeholder="请输入标题" :disabled="pageDialogObj.type == 'detail'" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs='24' :sm='24' :md='24' :lg='12' :xl='12'>
            <el-form-item label="信息类型" prop="typeId">
              <el-select v-model="form.typeId" placeholder="请选择信息类型" :disabled="!!pageDialogObj.id" clearable style="width: 100%;">
                <el-option v-for="item in treeData" :key="item.typeId" :label="item.typeName" :value="item.typeId" @click.native="selectNewsType(item)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs='24' :sm='24' :md='24' :lg='12' :xl='12'>
            <el-form-item label="多小区发布" label-width="96px" prop="shareFlag">
              <el-select v-model="form.shareFlag" placeholder="请选择" :disabled="pageDialogObj.type == 'detail' || (publicFlag == 'N' || !form.typeId) || pageDialogObj.type == 'edit'" clearable style="width: 100%;">
                <el-option v-for="dict in sysYesNo" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs='24' :sm='24' :md='24' :lg='12' :xl='12'>
            <el-form-item label="发布来源" prop="publishName">
              <el-input v-model="form.publishName" placeholder="请输入消息来源" :disabled="pageDialogObj.type == 'detail'" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs='24' :sm='24' :md='24' :lg='12' :xl='12'>
            <el-form-item label="起始时间" prop="time">
              <el-date-picker type="datetimerange" v-model="form.time" :disabled="pageDialogObj.type == 'detail'" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" start-placeholder="开始时间" end-placeholder="结束时间" :clearable="false" style="width: 100%;">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :xs='24' :sm='24' :md='24' :lg='12' :xl='12'>
            <el-form-item label="标题图片" prop="fileId">
              <el-upload
                v-if="!form.fileId"
                action="#"
                ref="upload"
                :limit="uploadObj.limit"
                :accept="uploadObj.accept"
                :show-file-list="false"
                :before-upload="beforeUpload"
              >
                <el-button slot="trigger" size="small" type="primary">上传</el-button>
              </el-upload>
              <div v-else class="img-container">
                <img :src="form.filePath" alt="">
                <div v-if="pageDialogObj.type != 'detail'" class="img-container-delete">
                  <span class="el-icon-delete" @click="handleDelete"></span>
                </div>
              </div>
            </el-form-item>
          </el-col>
          <el-col :xs='24' :sm='24' :md='24' :lg='24' :xl='24'>
            <el-form-item label="内 容" prop="context">
              <editor v-model="form.context" :readOnly="pageDialogObj.type == 'detail'" :key="refreshKey" :height="400"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="updateNotice-footer">
        <template v-if="pageDialogObj.type != 'detail'">
          <el-button size="small" @click="handleCancel">取消</el-button>
          <el-button type="primary" size="small" v-if="!loading" @click="handleConfirm">保存</el-button>
        </template>
        <el-button v-else size="small" @click="handleCancel">关闭</el-button>
      </div>
    </div>
    <div class="right-aside ql-editor" :style="{minWidth: device == 'mobile'?'100%':''}">
      <div class="modal-phone">
        <div class="phone-header">
          <div class="phone-header-top">
            <div class="phone-header-top-left">
              <svg-icon class="svgIcon right8" icon-class="signal" />
              <svg-icon class="svgIcon" icon-class="wifi" />
            </div>
            <div class="phone-header-top-right">
              <svg-icon class="svgIcon" icon-class="electrol" />
            </div>
          </div>
          <div class="phone-header-bottom">
            <div>新 闻</div>
            <img src="@/assets/images/mp.jpg" style="width: 60px;" alt="" srcset="">
          </div>
        </div>
        <div class="content">
          <div class="content-file" v-if="form.filePath">
            <img :src="form.filePath" alt="">
          </div>
          <div class="content-title">{{ form.title }}</div>
          <div class="content-text" v-html="form.context"></div>
          <div class="content-footer" v-if="form.publishName">
            发布：{{ form.publishName }}
          </div>
        </div>
      </div>
    </div>
    
  </div>
</template>
<script>
import news from '@/api/news'
import upload from '@/api/upload'

export default {
  props: {
    pageDialogObj: {
      type: Object,
      default: ({})
    }
  },
  data() {
    return {
      form: {},
      rules: {
        title: [{required: true, message: '标题不得为空', trigger: 'change'}],
        fileId: [{required: true, message: '标题图片不得为空', trigger: 'change'}],
        typeId: [{required: true, message: '信息类型不得为空', trigger: 'change'}],
        shareFlag: [{required: true, message: '发布场景不得为空', trigger: 'change'}],
        publishName: [{required: true, message: '发布来源不得为空', trigger: 'change'}],
        time: [{required: true, message: '起始时间不得为空', trigger: 'change'}],
        context: [{required: true, message: '发布内容不得为空', trigger: 'change'}],
      },
      treeData: [], /**信息类别列表 */
      refreshKey: '',
      uploadObj: {
        limit: 1,
        accept: '.jpg,.jpeg,.png',
        size: 500*1024*1024
      },
      publicFlag: '',
      loading: false
    }
  },
  computed: {
    userId() {
      return this.$store.getters.userId;
    },
    device() {
      return this.$store.getters.device;
    }
  },
  created() {
    this.getSysYesNo();
    this.getTreeList();
    if(this.pageDialogObj.id) {
      this.getDetailFn()
    } else {
      this.form = {
        title: '',
        typeName: '',
        shareFlag: 'N',
        time: '',
        context: '',
        publishName: '',
        noticesId: '',
        fileId: '',
        filePath: ''
      }
    }
  },
  methods: {
    async getTreeList() {
      const data = {
      }
      const res = await news.noticesTypeList(data);
      if(res.code == 200) {
        this.treeData = res.rows;
      }
    },
    handleCancel() {
      this.pageDialogObj.visible = false;
    },
    /**确定按钮提交功能*/
    handleConfirm() {
      this.submitForm('form', async ()=> {
        this.loading = true;
        const data = {
          title: this.form.title,
          typeId: this.form.typeId,
          shareFlag: this.form.shareFlag,
          publishName: this.form.publishName,
          startTime: this.form.time[0],
          endTime: this.form.time[1],
          context: this.form.context,
          noticesId: this.form.noticesId,
          fileId: this.form.fileId
        };
        let res;
        if(!this.pageDialogObj.id) {
          res = await news.addNotices(data);
        } else {
          res = await news.updateNotices(data);
        }
        if(res.code == 200) {
          this.$message.success('保存成功');
          this.pageDialogObj.visible = false;
          this.$emit('refresh');
        }
        setTimeout(() => {
          this.loading = false;
        }, 500);
      })
    },
    /**上传标题图片 */
    beforeUpload(file) {
      this.beforeUploadFn(file, this.uploadObj, async(newFile)=>{
        this.loading = true;
        const formData = new FormData();
        formData.append('file', newFile);
        const res = await upload.fileUpload(formData);
        if(res.code == 200) {
          this.form.fileId = res.data.id;
          this.form.filePath = res.data.url;
        }
        setTimeout(() => {
          this.loading = false;
        }, 500);
      })
    },
    /**删除标题图片 */
    handleDelete() {
      this.deleteFileFn(()=>{
        this.form.fileId = '';
        this.form.filePath = '';
      })
    },
    selectNewsType(obj) {
      this.form.shareFlag = obj.publicFlag;
      this.publicFlag = obj.publicFlag;
    },
    /**获取弹窗详情*/
    async getDetailFn() {
      this.loading = true;
      const data = {
        noticesId: this.pageDialogObj.id
      }
      const res = await news.noticesDetail(data);
      if(res.code == 200) {
        res.data.time = [res.data.startTime, res.data.endTime];
        this.form = res.data;
        this.refreshKey = new Date().getTime();
      }
      setTimeout(() => {
        this.loading = false;
      }, 500);
    }
  }
}
</script>

<style lang="scss">
.updateNotice {
  width: 100%;
  height: 100%;
  display: flex;
  .left-aside {
    width: 100%;
    padding: 40px 12px 8px;
    background: #fff;
    margin-right: 12px;
    overflow: auto;
    .el-form {
      margin: 0 auto;
    }
    .img-container {
      width: 200px;
      height: 140px;
      position: relative;
      img {
        width: 200px;
        height: 140px;
      }
      .img-container-delete {
        width: 100%;
        height: 100%;
        display: none;
        line-height: 140px;
        position: absolute;
        left: 0;
        top: 0;
        text-align: center;
        background:rgba($color: #000000, $alpha: 0.6);
        span {
          color: #fff;
          font-size: 16px;
          cursor: pointer;
        }
      }
      &:hover {
        .img-container-delete {
          display: block;
        }
      }
    }
  }
  .right-aside {
    width: 480px;
    overflow: scroll;
    flex-grow: 0;
    background: #fff;
    padding: 40px 0;
    .modal-phone {
      width: 320px;
      height: 90%;
      min-height: 560px;
      max-height: 620px;
      margin: auto;
      border: 1px solid #999;
      border-radius: 40px;
      padding: 0 0 16px 0;
      display: flex;
      flex-direction: column;
      overflow: auto;
      .phone-header {
        position: sticky;
        height: 56px;
        flex-shrink: 0;
        top: 0;
        z-index: 10;
        padding: 0 24px;
        border-radius: 40px 40px 0 0;
        box-shadow: 0 3px 6px -4px #eee;
        overflow: hidden;
        background: #fff;
        .phone-header-top {
          line-height: 28px;
          display: flex;
          justify-content: space-between;
          
        }
        .phone-header-bottom {
          text-align: right;
          display: flex;
          justify-content: space-between;
          div {
            width: 100%;
            transform: translateX(30px);
            text-align: center;
          }
        }
      }
      .content {
        width: 100%;
        height: 100%;
        overflow: auto;
        overflow-x: hidden;
        padding: 0 6px 0 8px;
        .content-file {
          width: 100%;
          img {
            width: 100%;
          }
        }
        .content-title {
          font-size: 18px;
          text-align: center;
          font-weight: bolder;
          padding-top: 24px;
        }
        .content-text {
          width: 100%;
          font-size: 12px;
          line-height: 24px;
          letter-spacing: 2px;
          img {
            width: 100% !important;
          }
        }
        .content-footer {
          text-align: right;
          font-size: 12px;
        }
      }
    }
  }
  
  .updateNotice-footer {
    width: 100%;
    line-height: 40px;
    background: #fff;
    position: sticky;
    bottom: -12px;
    text-align: center;
    z-index: 100;
  }
}
</style>